package GUIs;

import Configs.Config;
import Controllers.NhanVienController;
import Controllers.TaiKhoanController;
import com.db4o.ObjectSet;
import javax.swing.JOptionPane;
import models.NhanVien;
import models.Quyen;
import models.TaiKhoan;


public class f_taikhoan_them extends javax.swing.JFrame {

    private TaiKhoan tkthem;
    private Config conn;
    private TaiKhoanController tkc;
    private NhanVienController nvc;
    private ObjectSet<NhanVien> ds_nhanvien;
    private static f_taikhoan_them _instance;
    private f_taikhoan f_tk;
    
    public f_taikhoan_them(Config conn, f_taikhoan ftaikhoan) {
        this.conn = conn;
        this.tkc = new TaiKhoanController(this.conn);
        this.nvc = new NhanVienController(this.conn);
        this.f_tk = ftaikhoan;
        initComponents();
                
        ds_nhanvien = nvc.getAllNhanVien();
        for (NhanVien nhanvien : ds_nhanvien){
            cbnhanvien.addItem(nhanvien.getMSNV());
        }
    }
    
    public f_taikhoan getf_taikhoan(){
        return this.f_tk;
    }
    public f_taikhoan_them(Config conn, String msnv) {
        this.conn = conn;
        this.tkc = new TaiKhoanController(this.conn);
        this.nvc = new NhanVienController(this.conn);
        this.f_tk = null;
        initComponents();
    }
     public static f_taikhoan_them getInstance(Config conn,f_taikhoan ftaikhoan) {
        if (_instance == null) {
            _instance = new f_taikhoan_them(conn,ftaikhoan);
        }
        return _instance;
    }
     public static f_taikhoan_them getInstance(Config conn, String msnv) {
        if (_instance == null) {
            _instance = new f_taikhoan_them(conn,msnv);
        }
        return _instance;
    }
    public f_taikhoan_them() {
        initComponents();
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnchinh = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tftaikhoan = new javax.swing.JTextField();
        tfmatkhau = new javax.swing.JTextField();
        cbquyen = new javax.swing.JComboBox();
        btnthem = new javax.swing.JButton();
        btnlammoi = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnthoat = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        cbnhanvien = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm Tài Khoản");
        setResizable(false);

        jLabel1.setText("Tài Khoản : ");

        jLabel2.setText("Mật Khẩu : ");

        jLabel3.setText("Quyền : ");

        jLabel4.setText("Của NV : ");

        cbquyen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Quản Trị", "Nhân Viên" }));

        btnthem.setText("Thêm");
        btnthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthemActionPerformed(evt);
            }
        });

        btnlammoi.setText("Làm Mới");
        btnlammoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlammoiActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(204, 0, 0));
        jLabel5.setText("*");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(204, 0, 0));
        jLabel6.setText("*");

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("THÊM TÀI KHOẢN");

        cbnhanvien.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Chọn nhân viên --" }));

        javax.swing.GroupLayout pnchinhLayout = new javax.swing.GroupLayout(pnchinh);
        pnchinh.setLayout(pnchinhLayout);
        pnchinhLayout.setHorizontalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnchinhLayout.createSequentialGroup()
                .addContainerGap(127, Short.MAX_VALUE)
                .addComponent(cbnhanvien, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(72, 72, 72))
            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnchinhLayout.createSequentialGroup()
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnchinhLayout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4))
                            .addGap(30, 30, 30)
                            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tftaikhoan)
                                .addComponent(tfmatkhau)
                                .addComponent(cbquyen, 0, 195, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnchinhLayout.createSequentialGroup()
                            .addContainerGap(24, Short.MAX_VALUE)
                            .addComponent(btnthem, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnlammoi, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btnthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(15, 15, 15))
                        .addGroup(pnchinhLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addContainerGap()))
        );
        pnchinhLayout.setVerticalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnchinhLayout.createSequentialGroup()
                .addContainerGap(185, Short.MAX_VALUE)
                .addComponent(cbnhanvien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(64, 64, 64))
            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnchinhLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel8)
                    .addGap(26, 26, 26)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(tftaikhoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5))
                    .addGap(21, 21, 21)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(tfmatkhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6))
                    .addGap(21, 21, 21)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(cbquyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(27, 27, 27)
                    .addComponent(jLabel4)
                    .addGap(33, 33, 33)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnthem)
                        .addComponent(btnlammoi)
                        .addComponent(btnthoat))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(pnchinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(pnchinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthemActionPerformed
        if(cbnhanvien.getSelectedIndex()== 0 || tfmatkhau.getText().equals("") || tftaikhoan.getText().equals("") || tkc.checkexist(tftaikhoan.getText()))
             JOptionPane.showMessageDialog(this, "Vui lòng kiểm tra các thông tin", "Cảnh báo", JOptionPane.WARNING_MESSAGE);
        else{
            TaiKhoan tkthem = new TaiKhoan(tftaikhoan.getText(), tfmatkhau.getText(), (cbquyen.getSelectedItem().toString().equals("Quản Trị"))?Quyen.Quản_trị:Quyen.Nhân_viên, cbnhanvien.getSelectedItem().toString());
            this.tkc.insertTaiKhoan(tkthem, cbnhanvien.getSelectedItem().toString());
            JOptionPane.showMessageDialog(this, "Thêm tài khoản thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE );
            if(f_tk != null){
            this.f_tk.getMyFormat().clearTable(this.f_tk.getTbtk());
            tkc.showAllTaiKhoan(this.f_tk.getTbtk());
            }
            this.setVisible(false);
        }
        
    }//GEN-LAST:event_btnthemActionPerformed

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void btnlammoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlammoiActionPerformed
        tftaikhoan.setText("");
        tfmatkhau.setText("");
        cbnhanvien.setSelectedIndex(0);
        cbquyen.setSelectedIndex(0);
    }//GEN-LAST:event_btnlammoiActionPerformed

    /**
     * @param args the command line arguments
     */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlammoi;
    private javax.swing.JButton btnthem;
    private javax.swing.JButton btnthoat;
    private javax.swing.JComboBox cbnhanvien;
    private javax.swing.JComboBox cbquyen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel pnchinh;
    private javax.swing.JTextField tfmatkhau;
    private javax.swing.JTextField tftaikhoan;
    // End of variables declaration//GEN-END:variables
}
